/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ManagedConnection.hpp. Created on: 10.09.2009 17:24:13
 */

#ifndef MANAGEDCONNECTION_HPP_
#define MANAGEDCONNECTION_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"
#include "IManagedConnection.hpp"
#include "ConnectionHandler.hpp"
#include <set>

namespace smartobjects {

class ManagedConnection : public virtual IManagedConnection {
public:
	SO_DLLPRFX ManagedConnection();
	SO_DLLPRFX virtual ~ManagedConnection();

	SO_DLLPRFX virtual bool isOpen() ;

	SO_DLLPRFX virtual IEndpointLocation* getEndpointLocation();

	SO_DLLPRFX virtual ConnectionHandler* getConnectionHandlerToSend() ;
	SO_DLLPRFX virtual void send(ConnectionHandler* handler, const ByteBuffer* buffer);

	SO_DLLPRFX virtual void addConnectionListener(class IManagedConnectionListener*) ;
	SO_DLLPRFX virtual void delConnectionListener(class IManagedConnectionListener*) ;

	SO_DLLPRFX virtual void addStreamListener(class IManagedStreamListener*) ;
	SO_DLLPRFX virtual void delStreamListener(class IManagedStreamListener*) ;

	SO_DLLPRFX virtual void assignConnectionHandler(ConnectionHandler*) ;
	SO_DLLPRFX virtual void unassignConnectionHandler(ConnectionHandler*) ;

	SO_DLLPRFX virtual void fireOnConnected() ;
	SO_DLLPRFX virtual void fireOnConnected(ConnectionHandler* poHandler) ;

	SO_DLLPRFX virtual void fireOnConnectionFailure( const std::string& sErrorMessage) ;
	SO_DLLPRFX virtual void fireOnConnectionFailure(ConnectionHandler* poHandler, const std::string& sErrorMessage) ;

	SO_DLLPRFX virtual void fireOnDisconnected() ;
	SO_DLLPRFX virtual void fireOnDisconnected(ConnectionHandler* poHandler) ;

	SO_DLLPRFX virtual void fireOnReceived(ConnectionHandler* poHandler, ByteBuffer* buffer) ;

	SO_DLLPRFX virtual void fireOnReceiveError(ConnectionHandler* poHandler, const std::string& sErrorMessage) ;
	SO_DLLPRFX virtual void fireOnWriteError(ConnectionHandler* poHandler, const std::string& sErrorMessage) ;

	SO_DLLPRFX virtual ICoordinatorContext* getCoordinatorContext() ;
	SO_DLLPRFX virtual void setCoordinatorContext(ICoordinatorContext*) ;

protected:
	SO_DLLPRFX void processingPostActionConnectionListeners();
	SO_DLLPRFX void processingPostActionStreamListeners();

protected:
	IEndpointLocation* m_poEndpointLocation;
	IEndpointCoordinator* m_poEndpointCoordinator;
	boost::recursive_mutex m_mxEndpointLocation;

	typedef std::list<IManagedConnectionListener*> ConnectionListeners;
	ConnectionListeners m_aConnectionListeners;
	PostActionListenersContainer<IManagedConnectionListener> m_oPALConnectionListeners;
	boost::recursive_mutex m_mxConnectionListeners;

	typedef std::list<IManagedStreamListener*> StreamListeners;
	StreamListeners m_aStreamListeners;
	PostActionListenersContainer<IManagedStreamListener> m_oPALStreamListeners;
	boost::recursive_mutex m_mxStreamListeners;

	typedef std::set<ConnectionHandler*> Handlers;
	Handlers m_aHandlers;
	boost::recursive_mutex m_mxHandlers;

	bool m_bConnectionStatus;
	ICoordinatorContext* m_poCoordinatorContext;

};

}

#endif /* MANAGEDCONNECTION_HPP_ */
